package com.lz;

import com.lz.mapper.BrandMapper;
import com.lz.pojo.Brand;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class Test01 {
    @Test
    public void selectAll() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> users = mapper.selectAll();
        System.out.println(users);
        sqlSession.close();
    }
    @Test
    public void selectOne() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        List<Brand> users = mapper.selectOne( 1,"","");
        System.out.println(users);
        sqlSession.close();
    }
    @Test
    public void deleteOne() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        int users = mapper.deleteOne( 1);
        if (users!=0){
            System.out.println("删除成功");
        }else {
        System.out.println("删除失败");
        }
        sqlSession.close();
    }
    @Test
    public void addOne() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand sth=new Brand(1,"无奈又","无奈又有限公司",1,"无奈",0);
        mapper.addOne(sth);
//        int users=0;
//        if (users!=0){
//            System.out.println("添加成功");
//        }else {
//            System.out.println("添加失败");
//        }
        sqlSession.close();
    }
    @Test
    public void updateOne() throws IOException {
        String resource="MyBatis.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);
        Brand sth=new Brand(1,"无奈又","无奈又有限公司",100,"无奈llll",1);
        int users = mapper.updateOne(sth);
        if (users!=0){
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
        sqlSession.close();
    }
}
